﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using BoardPACDSWS.General.Logs;
using BoardPACUtil.Util;
using BoardPACDSBO.General;
using BoardPACDSDAO.LogOn;
using BoardPACDSBO.Base;
using BoardPACDAO.Base;

namespace BoardPACDSWS.Base
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AnonymousUserService" in code, svc and config file together.
    public class AnonymousUserService : IAnonymousUserService
    {
        public void IsDeviceAllowed(RequestHeader request)
        {
            try
            {
                Logs.EventLogger.Log("0," + LogPoint.Entry.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(request.RequestId=" + request.RequestId + ")");
                LogOnDAO logonDAO = new LogOnDAO();
                logonDAO.IsDeviceAllowed(request.deviceId, request.macAddress);
                Logs.EventLogger.Log("0," + LogPoint.Success.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(request.RequestId=" + request.RequestId + ")");
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(request.RequestId=" + request.RequestId + ")", e);
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("err_109," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(request.RequestId=" + request.RequestId + ")", e);
                throw new FaultException<GenFault>(new GenFault("err_109"), new FaultReason("err_109"));
            }
        }

        public int AddLog(string logInfo)
        {
            try
            {
                if (string.IsNullOrEmpty(logInfo))
                {
                    return -1;
                }

                AuditLogModel log = new AuditLogModel();
                string t = logInfo;
                string[] array = t.Split(',');

                if (array.Count() > 6)
                {
                    log.EventNumber = Convert.ToInt16(array[0].Trim());
                    log.EventOrError = array[1].Trim();
                    log.EventStatus = array[2].Trim();
                    log.ControllerName = array[3].Trim();
                    log.ActionName = array[4].Trim();
                    log.UserName = array[5].Trim();
                    log.Date = DateTime.Now;

                    string tempPara = "";
                    for (int i = 6; i < array.Count(); i++)
                    {
                        tempPara += array[i].Trim() + ", ";
                    }
                    char[] lop = { ' ', ',' };
                    tempPara = tempPara.TrimEnd(lop);
                    log.Parameters = tempPara;

                    int res = BaseDAO.AddLog(log);
                    return res;
                }
            }
            catch (GenServiceException e)
            {
                Logs.ErrorLogger.Log(e.Code + "," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(logInfo=" + logInfo + ")", e);
            }
            catch (Exception e)
            {
                Logs.ErrorLogger.Log("0," + LogPoint.Failure.ToString() + "," + Utility.GetAction(OperationContext.Current.IncomingMessageHeaders.Action) + "," + Global.UserName + ",Params(logInfo=" + logInfo + ")", e);
            }
            return -1;
        }

        public byte? GetIsDeviceAllowed(string deviceId, string macAddress)
        {
            try
            {
                LogOnDAO logonDAO = new LogOnDAO();
                return logonDAO.GetUserGuid(deviceId, macAddress);
            }
            catch (GenServiceException e)
            {
                throw new FaultException<GenFault>(new GenFault(e.Code), new FaultReason(e.Code));
            }
            catch (Exception e)
            {
                throw new FaultException<GenFault>(new GenFault("err_881"), new FaultReason("err_881"));
            }
        }
    }
}
